package com.oraro.paygatewaybiz.module.controller;

import com.oraro.common.result.Result;
import com.oraro.paygatewaybiz.dto.PayOrderParam;
import com.oraro.paygatewaybiz.exception.RRException;
import com.oraro.paygatewaybiz.module.entity.ChannelEntity;
import com.oraro.paygatewaybiz.module.entity.MchEntity;
import com.oraro.paygatewaybiz.module.entity.OrderEntity;
import com.oraro.paygatewaybiz.module.service.ChannelService;
import com.oraro.paygatewaybiz.module.service.MchService;
import com.oraro.paygatewaybiz.module.service.OrderService;
import com.oraro.paygatewaybiz.proxy.PayChannelServiceProxy;
import com.oraro.paygatewaybiz.utils.OrderValidator;
import com.oraro.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;


/**
 * @Author: jianghuiyong
 * @Date: 2018/11/19 14:20
 * @Version 1.0
 */
@RestController
@RequestMapping("/pay")
@Slf4j
public class PayOrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private MchService mchService;
    @Autowired
    private PayChannelServiceProxy payChannelServiceProxy;

    @PostMapping("createOrder")
    public Result createOrder (@Valid @RequestBody PayOrderParam payOrderParam) {

        MchEntity mchEntity = mchService.selectByMchId(payOrderParam.getMchId());
        if (mchEntity == null) {
            throw new RRException("商户服务号错误");
        } else if (mchEntity.getStatus() == 0){
            throw new RRException("商户服务已被禁用");
        }
        if (!OrderValidator.orderParamValidate(payOrderParam, mchEntity.getMchKey())) {
            throw new RRException("订单参数不合法");
        }
        if ("NATIVE".equals(payOrderParam.getTradeType()) && payOrderParam.getProductId() == null) {
            throw new RRException("订单参数错误");
        }

        OrderEntity orderEntity = new OrderEntity();
        try {
            BeanUtils.copyProperties(orderEntity, payOrderParam);
        } catch (IllegalAccessException e) {
            throw new RRException("订单参数异常");
        } catch (InvocationTargetException e) {
            throw new RRException("订单参数异常");
        }
        int num = orderService.getCountByTradeNo(orderEntity.getOutTradeNo());
        if (num > 0) {
            throw new RRException("该订单号已存在");
        }
        Long orderId = orderService.createOrder(orderEntity);
        return Result.success().put("orderId", orderId);
    }

    @GetMapping("checkOrder")
    public Result checkOrder(Long orderId) {
        OrderEntity orderEntity = orderService.getById(orderId);
        checkOrder(orderEntity);
        List<String> channels = channelService.getChannelType(orderEntity.getMchId());
        return Result.success().put("amount", orderEntity.getTotalFee()).put("channels",channels);
    }

    @PostMapping("payOrder")
    public Result payOrder (Long orderId, String payType) {
        OrderEntity orderEntity = orderService.getById(orderId);
        checkOrder(orderEntity);
        ChannelEntity channelEntity = channelService.selectByMchAndType(orderEntity.getMchId(), payType);
        if (channelEntity == null) {
            throw new RRException("非法支付渠道");
        } else {
            orderEntity.setChannelType(channelEntity.getChannelType());
            orderService.updateOrderChannel(orderEntity.getOrderId(), orderEntity.getChannelType());
        }
        Result result = payChannelServiceProxy.payOrder(orderEntity,channelEntity);

        return result;
    }

    private void checkOrder(OrderEntity orderEntity) {
        if (orderEntity == null) {
            throw new RRException("支付订单异常");
        }
        if (orderEntity.getTimeEnd().before(new Date())) {
            throw new RRException("支付订单已过期");
        }
        if (orderEntity.getPayStatus() == 1) {
            throw new RRException("支付订单已支付");
        }
    }
}
